/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bus.api;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.persistence.ResponseResult;
import com.thinkgem.jeesite.common.persistence.RestfulPage;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.bus.entity.*;
import com.thinkgem.jeesite.modules.bus.service.*;
import com.thinkgem.jeesite.modules.bus.web.VaccineController;
import com.thinkgem.jeesite.modules.sys.entity.Attachment;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.AttachmentService;
import com.thinkgem.jeesite.modules.sys.service.DictService;
import com.thinkgem.jeesite.modules.sys.service.LoginService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * restful demo
 * 所有的api返回值均为ResponseResult，在ResponseResult内赋予相关的对象
 *
 * @author zhangxingliang
 * @version 2019-06-12
 */
@RestController
@RequestMapping(value = "${wechatPath}/rest/vaccine")
public class RestVaccineController extends BaseController {

    @Autowired
    private VaccineService vaccineService;
    @Autowired
    private VaccineNoticeService vaccineNoticeService;

    @Autowired
    private AttachmentService attachmentService;
    @Autowired
    private VaccineInfoService vaccineInfoService;
    @Autowired
    private SubsidyStandardService subsidyStandardService;
    @Autowired
    private VaccineEntService vaccineEntService;
    @Autowired
    private VaccineManageService vaccineManageService;
    @Autowired
    private DictService dictService;


    /**
     * @Author zhangxingliang
     * @Description  疫苗补助信息
     * @Date 11:24 2019/6/12
     **/
    @RequestMapping(value = "get/{id}", method = RequestMethod.GET)
    public ResponseResult get(@PathVariable(value = "id") String id) throws Exception {
        Vaccine vaccine = this.vaccineService.getInfoAndAudit(id);
        List<Attachment> attachmentList = attachmentService.findListByTable(vaccine.getId(), "bus_vaccine");
        vaccine.setAttachmentList(attachmentList);
        VaccineInfo vaccineInfo = new VaccineInfo();
        vaccineInfo.setVaccineId(vaccine.getId());
        List<VaccineInfo> list = this.vaccineInfoService.findVaccineInfoiList(vaccineInfo);
        vaccine.setVaccineInfoList(list);
        return ResponseResult.success(vaccine);
    }
    
    /**
     * @Author zhangxingliang
     * @Description  编辑疫苗补助
     * @Date 11:43 2019/6/12
     **/
    @RequestMapping(value = "edit" )
    public ResponseResult editVaccine(@RequestBody Vaccine vaccine, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            this.vaccineService.edit(vaccine);
        } catch(Exception e) {
            return ResponseResult.failure("编辑失败，错误如下：" + e.getMessage());
        }
        return ResponseResult.success();
    }

    /**
     * @author Zhang Xingliang
     * @description 疫苗补助申请列表页直接提交
     * @date 20:14 2019/6/22
     * @params [vaccine, request, response]
     * @return com.thinkgem.jeesite.common.persistence.ResponseResult
     **/
    @RequestMapping(value = "submit" )
    public ResponseResult updateVaccine(@RequestBody Vaccine vaccine) throws Exception {
        return this.vaccineService.update(vaccine);
    }

    /**
     * @author Zhang Xingliang
     * @description 删除疫苗补助信息
     * @date 15:17 2019/6/22
     * @params [id]
     * @return com.thinkgem.jeesite.common.persistence.ResponseResult     
     **/
    @RequestMapping(value = "delete" )
    public ResponseResult deleteVaccine(@RequestBody Vaccine vaccine) throws Exception {
        return this.vaccineService.deleteVaccine(vaccine);
    }

    /**
     * @Author zhangxingliang
     * @Description  获取疫苗补助列表 
     * @Date 11:41 2019/6/12
     **/
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResponseResult list(@RequestBody Vaccine vaccine, HttpServletRequest request, HttpServletResponse response){
        Page<Vaccine> list = vaccineService.appVaccineList(new RestfulPage<Vaccine>(request, response), vaccine);
        return ResponseResult.success(list);
    }
    
    /**
     * @Author zhangxingliang
     * @Description  获取疫苗标准
     * @Date 10:53 2019/6/14
     **/
    @RequestMapping(value = "/standard", method = RequestMethod.POST)
    public ResponseResult standard(@RequestBody Vaccine vaccine){
        return ResponseResult.success(this.subsidyStandardService.findStandardByVaccineNameAndEntName(vaccine.getVaccineName(),vaccine.getFactory()));
    }

    /**
     * @author Zhang Xingliang
     * @description 根据疫苗厂家名获取疫苗补助标准 list
     * @date 16:07 2019/6/22
     * @params [vaccine]
     * @return com.thinkgem.jeesite.common.persistence.ResponseResult
     **/
    @RequestMapping(value = "/standardList", method = RequestMethod.POST)
    public ResponseResult standardList(@RequestBody Vaccine vaccine){
        User user = UserUtils.getUser();
        if(null == user){
            return ResponseResult.failure("未登录");
        }
        vaccine.setEntId(user.getId());
        return ResponseResult.success(this.subsidyStandardService.findStandardByEntName(vaccine));
    }

    /**
     * @Author zhangxingliang
     * @Description  获取所有疫苗
     * @Date 16:38 2019/6/15
     **/
    @RequestMapping(value = "/vaccineList", method = RequestMethod.GET)
    public ResponseResult findVaccineManageList(){
        User user = UserUtils.getUser();
        if(null == user){
            return null;
        }
        List<Map<String,Object>> list = this.vaccineManageService.findVaccineManageList(user.getId());
        return ResponseResult.success(list);
    }

    /**
     * @Author zhangxingliang
     * @Description  获取所有疫苗厂家
     * @Date 16:38 2019/6/15
     **/
    @RequestMapping(value = "/vaccineEntList", method = RequestMethod.GET)
    public ResponseResult findVaccineEntList(){
        List<Map<String,Object>> list = this.vaccineEntService.findVaccineEntList();
        return ResponseResult.success(list);
    }

    /**
     * @Author zhangxingliang
     * @Description 一次全部获取疫苗补助新增页面 需要下拉加载数据
     * @Date 13:31 2019/6/17
     **/

    @RequestMapping(value = "/vaccineCode", method = RequestMethod.GET)
    public ResponseResult findVaccineCode(){
        User user = UserUtils.getUser();
        if(null == user){
            return null;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("vaccineEnt",this.vaccineEntService.findVaccineEntList());
        map.put("vaccineManage",this.vaccineManageService.findVaccineManageList(user.getId()));
        Dict dict = new Dict();
        dict.setType("mlType");
        map.put("mlType",this.dictService.searchListByType(dict));
//        dict.setType("calcUnitType");
//        map.put("calcUnit",this.dictService.searchListByType(dict));
        dict.setType("vaccineType");
//        map.put("vaccineType",this.dictService.searchListByType(dict));
        map.put("vaccineType",this.dictService.findTextDictByType(dict));
        return ResponseResult.success(map);
    }

    /**
     * @author Zhang Xingliang
     * @description 疫苗补助发票唯一校验
     * @date 17:14 2019/7/24 0024
     * @param vaccine
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    @RequestMapping(value = "/checkInvoice", method = RequestMethod.POST)
    public ResponseResult checkInvoice(@RequestBody Vaccine vaccine){
        return ResponseResult.success(this.vaccineService.checkInvoice(vaccine.getInvoiceNo()));
    }

    /**
     * @author Zhang Xingliang
     * @description 获取疫苗补助须知内容
     * @date 11:34 2019/8/12
     * @param vaccineNotice
     * @return com.thinkgem.jeesite.common.persistence.ResponseResult
     **/
    @RequestMapping(value = "/findVaccineNotice", method = RequestMethod.POST)
    public ResponseResult findVaccineNotice(@RequestBody VaccineNotice vaccineNotice){
        return ResponseResult.success(vaccineNoticeService.findVaccineNotice(vaccineNotice));
    }

}